package cn.com.cym.gamezao.service.recruit.impl;

import cn.com.cym.gamezao.base.BaseImpl;
import cn.com.cym.gamezao.base.BaseRepository;
import cn.com.cym.gamezao.base.ResultResponse;
import cn.com.cym.gamezao.base.SearchPage;
import cn.com.cym.gamezao.entity.authorize.User;
import cn.com.cym.gamezao.entity.xmu.recruit.Certificate;
import cn.com.cym.gamezao.exception.BaseException;
import cn.com.cym.gamezao.jwt.RequestUser;
import cn.com.cym.gamezao.repository.xmu.recruit.CertificateRepository;
import cn.com.cym.gamezao.service.recruit.CertificateService;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
@AllArgsConstructor
public class CertificateImpl extends BaseImpl<Certificate> implements CertificateService {

    final CertificateRepository thisRepository;

    @Override
    public BaseRepository<Certificate, Long> getJpaRepository() {
        return thisRepository;
    }

    @Override
    public Page<Certificate> findByPageable(SearchPage searchPage) {
        // 不做分页
        return null;
    }

    @Override
    public Certificate getByUserId(Long userId) {
        final List<Certificate> allByUser = this.thisRepository.getAllByUser((User) new User().setId(userId));
        if(allByUser.size()>0){
            return allByUser.get(0);
        }
        return null;
    }

    @Override
    public Certificate getByUser() {
        final List<Certificate> allByUser = this.thisRepository.getAllByUser(RequestUser.get());
        if(allByUser.size()>0){
            return allByUser.get(0);
        }
        return null;
    }

    @Override
    public Certificate findAllByUser(User user) {
        final List<Certificate> allByUser = this.thisRepository.getAllByUser(user);
        if(allByUser.size()>0){
            return allByUser.get(0);
        }
        return null;
    }

    @Override
    public Certificate save(Certificate certificate) {
        //判断是否存在
        User user = RequestUser.get();
        final List<Certificate> allByUser = this.thisRepository.getAllByUser(user);
        if(allByUser.size()==0 && certificate.getId()==null){
            certificate.setUser(user);
        }else if(certificate.getId()!=null){
            final Certificate byId = this.thisRepository.getById(certificate.getId());
            if(allByUser.size() == 0 &&  !byId.getUser().getId().equals(user.getId())){
               throw new BaseException("当前账号不允许修改他人数据",new ResultResponse("当前账号不允许修改他人数据", HttpStatus.BAD_REQUEST.value()));
            }else {
                for(Certificate cert: allByUser){
                    if(!cert.getUser().getId().equals(user.getId())){
                        throw new BaseException("当前账号不允许修改他人数据",new ResultResponse("当前账号不允许修改他人数据", HttpStatus.BAD_REQUEST.value()));
                    }
                }

            }
            certificate.setUser(user);
        }
        return super.save(certificate);
    }
}
